package com.se.utils;

import com.se.common.Message;
import com.se.messageBox.InformationAlert;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

public class ClientUtils {
    private ClientUtils() {
    }

    public static void information_Alert(String message) {
        new InformationAlert(message).show();
    }

    public static void sendMessage(Socket socket, Message message) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(message);
        System.out.println("消息发送者为: " + message.getSendUser());
        System.out.println("消息接收者为: " + message.getReceiveUser());
    }

    public static void showMainStage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/mainView/MainView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            scene.getStylesheets().add(ClientUtils.class.getResource("/com/se/css/mention.css").toExternalForm());
            Stage mainStage = new Stage();
            mainStage.initStyle(StageStyle.UNDECORATED);
            mainStage.getIcons().add(new Image(Objects.requireNonNull(ClientUtils.class.getResourceAsStream("/image/Icon.png"))));
            mainStage.setTitle("聊天");
            mainStage.setScene(scene);
            mainStage.setOnCloseRequest(event -> {
                ServiceUtils.closeConnection();
                StageManager.remove(StageType.MAIN_STAGE);
                Platform.exit();
            });

            mainStage.focusedProperty().addListener((obs, oldVal, newVal) -> {
                if (newVal) {
                    closeOtherStage();
                }
            });

            StageManager.add(StageType.MAIN_STAGE, mainStage);
            mainStage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showLoginStage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/Login/Login.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setTitle("登录");
            stage.setResizable(false);

            stage.getIcons().add(new Image(Objects.requireNonNull(ClientUtils.class.getResourceAsStream("/image/icon.png"))));

            AtomicReference<Double> offsetX = new AtomicReference<>((double) 0);
            AtomicReference<Double> offsetY = new AtomicReference<>((double) 0);
            scene.setOnMousePressed(event->{
                offsetX.set(event.getSceneX());
                offsetY.set(event.getSceneY());
            });

            scene.setOnMouseDragged(event->{
                stage.setX(event.getScreenX()-offsetX.get());
                stage.setY(event.getScreenY()-offsetY.get());
            });
            stage.setOnCloseRequest(event -> {
                ServiceUtils.closeConnection();
                StageManager.remove(StageType.LOGIN_STAGE);
                Platform.exit();
            });

            StageManager.add(StageType.LOGIN_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showRegisterStage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/Register/Register.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage registerStage = new Stage();
            registerStage.setTitle("注册");
            registerStage.setScene(scene);
            registerStage.initStyle(StageStyle.UNDECORATED);
            registerStage.setResizable(false);
            registerStage.setOnCloseRequest(event -> {
                ServiceUtils.closeConnection();
                StageManager.remove(StageType.REGISTER_STAGE);
                Platform.exit();
            });

            registerStage.getIcons().add(new Image(Objects.requireNonNull(ClientUtils.class.getResourceAsStream("/image/Icon.png"))));
            AtomicReference<Double> offsetX = new AtomicReference<>((double) 0);
            AtomicReference<Double> offsetY = new AtomicReference<>((double) 0);
            scene.setOnMousePressed(event->{
                offsetX.set(event.getSceneX());
                offsetY.set(event.getSceneY());
            });
            scene.setOnMouseDragged(event->{
                registerStage.setX(event.getScreenX()-offsetX.get());
                registerStage.setY(event.getScreenY()-offsetY.get());
            });

            StageManager.add(StageType.REGISTER_STAGE, registerStage);
            registerStage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showOptionStage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/OptionView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setTitle("设置");
            stage.setScene(scene);
            stage.setResizable(false);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.OPTION_STAGE));

            StageManager.add(StageType.OPTION_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showCreateGroupPage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/createGroupView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.CREATE_GROUP_STAGE));

            StageManager.add(StageType.CREATE_GROUP_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showJoinGroupPage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/JoinGroupView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.JOIN_GROUP_STAGE));

            StageManager.add(StageType.JOIN_GROUP_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showAddRobotPage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/addRobotView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setResizable(false);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.ADD_ROBOT_STAGE));

            StageManager.add(StageType.ADD_ROBOT_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showAddFriendPage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/addFriendView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setResizable(false);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.ADD_FRIEND_STAGE));

            StageManager.add(StageType.ADD_FRIEND_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void showFriendRequestPage() {
        FXMLLoader fxmlLoader = new FXMLLoader(ClientUtils.class.getResource("/com/se/optionView/friendRequestView.fxml"));
        try {
            Scene scene = new Scene(fxmlLoader.load());
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setResizable(false);
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setOnCloseRequest(event -> StageManager.remove(StageType.FRIEND_REQUEST_STAGE));

            StageManager.add(StageType.FRIEND_REQUEST_STAGE, stage);
            stage.show();
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public static void closeOtherStage() {
        if(StageManager.get(StageType.CREATE_GROUP_STAGE) != null) {
            StageManager.get(StageType.CREATE_GROUP_STAGE).close();
            StageManager.remove(StageType.CREATE_GROUP_STAGE);
        }

        if(StageManager.get(StageType.JOIN_GROUP_STAGE) != null) {
            StageManager.get(StageType.JOIN_GROUP_STAGE).close();
            StageManager.remove(StageType.JOIN_GROUP_STAGE);
        }
    }
}
